import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
import java.io.*;
import javax.swing.ImageIcon;


public class DatabaseGUI extends JFrame implements ActionListener
{   
	private static final long serialVersionUID = 1L;
	private static final int PREFERRED_WIDTH = 1000;
    private static final int PREFERRED_HEIGHT = 1000;
    private static final Dimension PREFERRED_SIZE =
    new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT);
    private JFrame frame;
    public final static boolean RIGHT_TO_LEFT = true;

    public DatabaseGUI()
    {
        guiFrame();
        //
    }
        
        /**
         * Accounting Layout
         */
        public Container accounting()
        {
            // Ch1:
            Container contentPane1 = new Container();
            GridLayout grid2 = new GridLayout(0,8);
            contentPane1.setLayout(grid2);
            
            // Ch2:
            ImageIcon is = createImageIcon("images/incomeStatement.jpg");
            ImageIcon bs = createImageIcon("images/balanceSheet.jpg");
            ImageIcon cf = createImageIcon("images/cashFlow.gif");
            ImageIcon eps = createImageIcon("images/eps.gif"); 
            ImageIcon pe = createImageIcon("images/pe.gif");
            Container contentPane = new Container();
            GridLayout grid1 = new GridLayout(3,2);
            contentPane.setLayout(grid1);
            
            JButton b1 = new JButton("Income Statement", is);
            b1.setVerticalTextPosition(AbstractButton.BOTTOM);
            b1.setHorizontalTextPosition(AbstractButton.CENTER);
            b1.setActionCommand("b1");
            contentPane.add(b1);
            
            JButton b2 = new JButton("Balance Sheet", bs);
            b2.setVerticalTextPosition(AbstractButton.BOTTOM);
            b2.setHorizontalTextPosition(AbstractButton.CENTER);
            b2.setActionCommand("b2");
            contentPane.add(b2);
            
            JButton b3 = new JButton("Cash Flow Statement", cf);
            b3.setVerticalTextPosition(AbstractButton.BOTTOM);
            b3.setHorizontalTextPosition(AbstractButton.CENTER);
            b3.setActionCommand("b3");
            contentPane.add(b3);
            
            JButton b4 = new JButton("Earning per share", eps);
            b4.setVerticalTextPosition(AbstractButton.BOTTOM);
            b4.setHorizontalTextPosition(AbstractButton.CENTER);
            b4.setActionCommand("b4");
            contentPane.add(b4);
            
            JButton b5 = new JButton("P/E Ratio", pe);
            b5.setVerticalTextPosition(AbstractButton.BOTTOM);
            b5.setHorizontalTextPosition(AbstractButton.CENTER);
            b5.setActionCommand("b5");
            contentPane.add(b5);
             
            b1.setToolTipText("It displays " + 
            "the revenues recognized for a specific period, and the cost and expenses " +
            "including write-offs (depreciation and " + 
            "amortization of various assets) and taxes."); 
            b2.setToolTipText("Assets = Liabilities + Shareholders' Equity");
            b3.setToolTipText("The amounts of cash and cash equivalents entering and leaving a company.");
            b4.setToolTipText(" [Net Income - Dividends on Preferred Stocks] / Average Outstanding Shares");
            b5.setToolTipText("Market Value per Share/ Earnings per Share(EPS)");
            b4.addActionListener(this);
            b3.addActionListener(this);
            b2.addActionListener(this);
            b1.addActionListener(this);
            b5.addActionListener(this);
            return contentPane;
        }
        
        /**
         * Accounting Actions
         */
        public void actionPerformed(ActionEvent e)
        {
            if ("b5".equals(e.getActionCommand())) 
            {
                AccountingSBS accounting = new AccountingSBS();
                accounting.peRatio();
            }
            if ("b1".equals(e.getActionCommand()))
            {

            }
                        
            if ("b2".equals(e.getActionCommand()))
            {
            	AccountingSBS accounting = new AccountingSBS();
                accounting.firstEntry();
            }
                
                if ("b3".equals(e.getActionCommand()))
                {
                	
                }
                if ("b4".equals(e.getActionCommand()))
                {
                
                }
                }

        public Container finance()
        {
            JPanel panel = new JPanel();
            return panel;
        }
        
        public Container businessLaw()
        {
            // The area where the details will be displayed.
            final JTextArea details = new JTextArea(10, 50);
            details.setEditable(true);
            JScrollPane scrollArea =
                    new JScrollPane(details,
                                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
        // Set up the buttons.
        JPanel buttonArea = new JPanel();
        JButton saveAs = new JButton("Save");
        JButton load = new JButton("Load");
        JButton clear = new JButton("Clear Everything");
        clear.setToolTipText("Careful: Clears all text!");
        
        // List all of the entries.
        saveAs.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev)
            {
                String notes = details.getText();
                JFileChooser fileSave = new JFileChooser();
                fileSave.showSaveDialog(frame);
                save(fileSave.getSelectedFile(), notes);
            }
         });
         
         // loads to the details area.
        load.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev)
            {
                JFileChooser fileOpen = new JFileChooser();
                fileOpen.showOpenDialog(frame);
                load(fileOpen.getSelectedFile(),details);
                //to be implemented. Need to store the variable
                // in another variable and then use it to limit 
                // the words from exceeding the pages/.
            }
        });
        
        // Clear the details area.
        clear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev)
            {
                details.setText("");
            }
        });
        
        buttonArea.add(saveAs);
        buttonArea.add(load);
        buttonArea.add(clear);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scrollArea, BorderLayout.CENTER);
        panel.add(buttonArea, BorderLayout.SOUTH); 
        
        return panel;
        }
        
        public Container statistics()
        {
            JPanel panel = new JPanel();
            return panel;
        }
        
        public Container notes()
        {
            // The area where the details will be displayed.
            final JTextArea details = new JTextArea(10, 50);
            details.setEditable(true);
            JScrollPane scrollArea =
                    new JScrollPane(details,
                                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
            // Set up the buttons.
        JPanel buttonArea = new JPanel();
        JButton saveAs = new JButton("Save");
        JButton clear = new JButton("Clear Everything");
        clear.setToolTipText("Careful: Clears all text!");
        
        // List all of the entries.
        saveAs.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev)
            {
                String notes = details.getText();
                JFileChooser fileSave = new JFileChooser();
                fileSave.showSaveDialog(frame);
                save(fileSave.getSelectedFile(), notes);
            }
         });
    // Clear the details area.
        clear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev)
            {
                details.setText("");
            }
        });
        buttonArea.add(saveAs);
        buttonArea.add(clear);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scrollArea, BorderLayout.CENTER);
        panel.add(buttonArea, BorderLayout.SOUTH); 
        
        return panel; 
        }
        
        /**
         *  Returns an ImageIcon, or null if the path was invalid. 
         */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = DatabaseGUI.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
        
        public static void main(String[] args)
        {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            DatabaseGUI gui = new DatabaseGUI();
            gui.setVisible(true);
        }});
        }
        
        private void save(File file, String text)
        {
            try { 
                RandomAccessFile rf = new RandomAccessFile (file, "rw");
                rf.writeBytes(text);
                rf.close ();  
            } catch(Exception ex) {}
        }
        
        private void load(File file, JTextArea details)
        {
            try{
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                StringBuffer sb = new StringBuffer();
                String eachLine = br.readLine();
                while (eachLine != null) {
                    sb.append(eachLine);
                    sb.append("\n");
                    eachLine = br.readLine();
                    details.setText(sb.toString());
                }
            }catch(Exception e){}
        }
        
        public void guiFrame()
        {
        	frame = new JFrame();
            setTitle("BBA1 Subjects");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent ev)
                {
                    setVisible(false);
                }
            });
            final Container contentPane = getContentPane();
            JTabbedPane tabbedArea = new JTabbedPane();
            tabbedArea.add("Accounting",accounting()); 
            tabbedArea.add("Finance",finance());
            tabbedArea.add("Business Law",businessLaw());
            tabbedArea.add("Statistics",statistics());
            tabbedArea.add("Notes",notes());
            contentPane.add(tabbedArea);
            setVisible(true);
            setSize(PREFERRED_SIZE);
        }
        
    }